//////// comments ///////

#include <vector>
#include "combined.h"
#include "command.h"
#include "commandTable.h"
#include "definitions.h"
#include "log.h"
#include "stringutil.h"
#include "timestamp.h"
#include "world.h"

// pCommand definitions
// (Keep in alphabetical order... it's MUCH easier)
pCmdAge::pCmdAge( void ) {
	setName( "age" );
	setLevel( GOD );
	setEnabled( true );	
	setOmniset( false );
	setTags( "pedit" );
	addSyntax( "syntax: age <value>   {Cset the players age to the given value" );
}

bool pCmdAge::execute( Character* character, const std::string& args ) {
	Character* p = character->getCharacter();

	if ( args.empty() ) {
		character->Send( getSyntax() );
		return false;
	}

	p->setAge( atoi( args.c_str() ) );
	p->Send( "Your age changed to " + to_str( p->getAge() ) + "." );
	character->Send( p->getName() + "'s age set to " + to_str( p->getAge() ) + "." );

	return true;
}

pCmdCommands::pCmdCommands( void ) {
	setName( "commands" );
	setLevel( 1 );
	setEnabled( true );
	setOmniset( false );
	setTags( "pedit" );
	addSyntax( "sytnax: commands    {Clist all current commands" );
}

bool pCmdCommands::execute( Character* character, const std::string& args ) {
	std::string buf;
	std::string temp;
	unsigned i=0;

	// HEAD Commands...
	if ( character->getLevel() >= HEAD ) {
		buf << "\n\r\t{cHead commands:{x\n\r";
		for ( std::vector< Command* >::iterator it = pCommandTable::Instance().getCommands().begin(); it != pCommandTable::Instance().getCommands().end(); ++it ) {
			if ( (*it)->getLevel() == HEAD ) {
				if ( i % 6 == 0 ) {
					if ( i )
						buf << "\n\r";
					buf << "  ";
				}
				if ( (*it)->getEnabled() )
					buf << format( 12, (*it)->getName(), temp );
				else
					buf << format( 12, (*it)->getName(), temp, 'w' );
				i++;
			}
		}
	}

	// GOD Commands...
	if ( character->getLevel() >= GOD ) {
		i = 0;
		buf << "\n\r\t{cGod commands:{x\n\r";
		for ( std::vector< Command* >::iterator it = pCommandTable::Instance().getCommands().begin(); it != pCommandTable::Instance().getCommands().end(); ++it ) {
			if ( (*it)->getLevel() == GOD ) {
				if ( i % 6 == 0 ) {
					if ( i )
						buf << "\n\r";
					buf << "  ";
				}
				if ( (*it)->getEnabled() )
					buf << format( 12, (*it)->getName(), temp );
				else
					buf << format( 12, (*it)->getName(), temp, 'w' );
				i++;
			}
		}
	}

	// BUILDER Commands...
	if ( character->getLevel() >= BUILDER ) {
		i = 0;
		buf << "\n\r\t{cBuilder commands:{x\n\r";
		for ( std::vector< Command* >::iterator it = pCommandTable::Instance().getCommands().begin(); it != pCommandTable::Instance().getCommands().end(); ++it ) {
			if ( (*it)->getLevel() == BUILDER ) {
				if ( i % 6 == 0 ) {
					if ( i )
						buf << "\n\r";
					buf << "  ";
				}
				if ( (*it)->getEnabled() )
					buf << format( 12, (*it)->getName(), temp );
				else
					buf << format( 12, (*it)->getName(), temp, 'w' );
				i++;
			}
		}
	}

	// Regular Commands...
	buf << "\n\r\t{c-- Commands --{x \n\r";
	i = 0;
	for ( std::vector< Command* >::iterator it = pCommandTable::Instance().getCommands().begin(); it != pCommandTable::Instance().getCommands().end(); ++it ) {
		if ( (*it)->getLevel() == 1 ) {
			if ( i % 6 == 0 ) {
				if ( i )
					buf << "\n\r";
				buf << "  ";
			}
			if ( (*it)->getEnabled() )
				buf << format( 12, (*it)->getName(), temp );
			else
				buf << format( 12, (*it)->getName(), temp, 'w' );
			i++;
		}
	}

	character->Send( buf );

	return true;
}

pCmdExit::pCmdExit( void ) {
	setName( "exit" );	
	setLevel( 1 );
	setEnabled( true );
	setOmniset( true );
	setTags( "pedit" );
	addSyntax( "syntax: exit     {Cexit pedit mode" );
}

bool pCmdExit::execute( Character* character, const std::string& args ) {

	character->setCharacter( NULL );
	character->setStatus( CONNECTED );
	character->Send( "You have exited pedit mode." );

	return true;
}

pCmdEcho::pCmdEcho( void ) {
	setName( "echo" );
	setLevel( GOD );
	setEnabled( true );
	setOmniset( false );
	setTags( "pedit" );
	addSyntax( "syntax: echo <value>   {Csets the players gecho color to the given value" );
}

bool pCmdEcho::execute( Character* character, const std::string& args ) {
	Character* p = character->getCharacter();
	char color = trim( args )[0];

	if ( args.empty() ) {
		character->Send( getSyntax() );
		return false;
	}

	if ( p->getLevel() < GOD ) {
		character->Send( p->getName() + " doesn't have gecho." );
		return false;
	}

	p->setEcho( color );
	p->Send( "Your gecho color has been changed to " + to_str( p->getEcho() ) + " ({" + to_str( p->getEcho() ) + "color{x)" );
	character->Send( p->getName() + "'s gecho color changed to " + to_str( p->getEcho() ) + " ({" + to_str( p->getEcho() ) + "color{x)" );

	return true;
}

pCmdGender::pCmdGender( void ) {
	setName( "gender" );
	setLevel( GOD );
	setEnabled( true );	
	setOmniset( false );
	setTags( "pedit" );
	addSyntax( "syntax: gender <value>   {Csets the players gender to the given value" );
}

bool pCmdGender::execute( Character* character, const std::string& args ) {
	std::string s_gender = trim( args );
	Character* p = character->getCharacter();

	if ( s_gender.empty() ) {
		character->Send( getSyntax() );
		return false;
	}

	p->setGender( Creature::string2gender( s_gender ) );
	p->Send( "Your gender has been changed to " + Creature::gender2string( p->getGender() ) + "." );
	character->Send( p->getName() + "'s gender changed to " + Creature::gender2string( p->getGender() ) + "." );
	return true;
}

pCmdLname::pCmdLname( void ) {
	setName( "lname" );
	setLevel( GOD );
	setEnabled( true );	
	setOmniset( false );
	setTags( "pedit" );
	addSyntax( "syntax: lname <string>  {Csets the players last name to the given string" );
}

bool pCmdLname::execute( Character* character, const std::string& args ) {
	std::string lname = trim( args );
	Character* p = character->getCharacter();

	if ( lname.empty() ) {
		character->Send( getSyntax() );
		return false;
	}

	p->setLastName( lname );
	p->Send( "Your last name has been changed to " + p->getLastName() + "." );
	character->Send( p->getName() + "'s last name changed to " + p->getLastName() + "." );

	return true;
}

pCmdName::pCmdName( void ) {
	setName( "name" );
	setLevel( GOD );
	setEnabled( true );	
	setOmniset( false );
	setTags( "pedit" );
	addSyntax( "syntax: name <string>  {Csets the players name to the given string" );
}

bool pCmdName::execute( Character* character, const std::string& args ) {
	std::string name = trim( args );
	Character* p = character->getCharacter();

	if ( name.empty() ) {
		character->Send( getSyntax() );
		return false;
	}

	p->setName( name );
	p->Send( "Your name has been changed to " + p->getName() + "." );
	character->Send( "Player name changed to " + p->getName() + "." );

	return true;
}
